package com.hanyxx.thread;

import lombok.SneakyThrows;

public class Demo12 {
    public static void main(String[] args) throws InterruptedException {
        Object obj = new Object();
        Demo12ThreadA threadA = new Demo12ThreadA(obj);
        threadA.start();
        //让主线程等待2秒钟
        Thread.sleep(2000);
        Demo12ThreadB threadB = new Demo12ThreadB(obj);
        threadB.start();
    }
}
class Demo12ThreadA extends  Thread{
    private Object lock;

    public  Demo12ThreadA(Object lock){
        setName("A线程");
        this.lock = lock;
    }

    @SneakyThrows
    @Override
    public void run() {
        synchronized (lock){
            System.out.println(Thread.currentThread().getName()+"进入等待，时间为："+System.currentTimeMillis());
            lock.wait();
            System.out.println(Thread.currentThread().getName()+"结束等待，时间为："+System.currentTimeMillis());
        }
    }
}
class Demo12ThreadB extends  Thread{
    private Object lock;

    public  Demo12ThreadB(Object lock){
        setName("B线程");
        this.lock = lock;
    }

    @SneakyThrows
    @Override
    public void run() {
        synchronized (lock){
            System.out.println(Thread.currentThread().getName()+"进行通知，时间为："+System.currentTimeMillis());
            lock.notify();
            Thread.sleep(1000);
        }
    }
}
